﻿using Csb.SignalRClient2.Model;
using Microsoft.AspNetCore.SignalR.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Csb.SignalClient2
{
    public partial class LoginView : Form
    {
        private HubConnection _hubConnection;
        private User User { get; set; }
        public LoginView()
        {
            InitializeComponent();
        }

        private async void login_Click(object sender, EventArgs e)
        {
            //客户端调用服务器更新数据

            if (string.IsNullOrEmpty(this.userName.Text.Trim()) || string.IsNullOrEmpty(this.password.Text.Trim()))
            {
                MessageBox.Show("用户名或密码不能为空！");
                return;
            }
            User = new User();
            User.UserName = this.userName.Text.Trim();
            User.UserPassord = this.password.Text.Trim();

            ////调用服务端，ChatHub类中的方法UpdateMessage，接收客户端发送的更新数据
            using (HttpClient client = new HttpClient())
            {
                var json = System.Text.Json.JsonSerializer.Serialize(User);
                var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
                var response = await client.PostAsync("http://127.0.0.1:8081/Account/Login", content);

                if (response.IsSuccessStatusCode)
                {
                    Form1 mainForm = new Form1(User);
                    mainForm.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("登录失败！");
                }
            }

        }
    }
}
